package com.pet.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.backend.mapper.ContactMapper;
import com.pet.backend.mapper.HomeMapper;
import com.pet.backend.pojo.Contact;
import com.pet.backend.pojo.Home;
import com.pet.backend.pojo.Orders;
import com.pet.backend.service.IContactService;
import com.pet.backend.service.IHomeService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pet
 * @since 2024-07-13
 */
@Service
public class HomeServiceImpl extends ServiceImpl<HomeMapper, Home> implements IHomeService {
    // 订单
    @Resource
    private OrdersServiceImpl ordersService;

    // 商品
    @Resource
    private GoodsServiceImpl goodsService;

    // 会员
    @Resource
    private UserServiceImpl userService;

    // 联系我们
    @Resource
    private ContactServiceImpl contactService;

    // 留言
    @Resource
    private MessageServiceImpl messageService;

    // 管理员
    @Resource
    private UserBackendServiceImpl userBackendService;

    @Override
    public Home queryHome() {
        Home home = new Home();
        // 订单数
        int orderCount = ordersService.count();
        home.setOrderCount(orderCount);
        // 总订单金额数
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.select("IFNULL(sum(totalprice),0) as totalPrice");
        Map<String, Object> map = ordersService.getMap(wrapper);
        home.setOrderPrice((BigDecimal) map.get("totalPrice"));
        // 商品数
        int goodsCount = goodsService.count();
        home.setGoodsCount(goodsCount);
        // 会员数
        int userCount = userService.count();
        home.setUserCount(userCount);
        // 联系我们数量
        int contactCount = contactService.count();
        home.setContactCount(contactCount);
        // 留言数
        int messageCount = messageService.count();
        home.setMessageCount(messageCount);
        return home;
    }
}
